﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VmcRemote.Common;
using System.Threading;

namespace VmcRemote.PpcRemote
{
    public partial class RemoteForm : Form
    {      

        delegate void StatusChangedDelegate(object sender, ConnectionStatusChangedEventArgs e);
        delegate void ErrorOccurredDelegate(object sender, RemoteErrorEventArgs e);

        Remote remote;

        public RemoteForm()
        {
            InitializeComponent();
        }

        #region Form Events
        private void RemoteForm_Load(object sender, EventArgs e)
        {
            SettingsForm form = new SettingsForm();
            form.ShowDialog();

            if (form.Settings == null)
            {
                this.Close();
            }
            else
            {
                VmcRemote.Common.Interfaces.IConnection connection = form.Settings.CreateConnection();
                // subscribing directly to this event is a hack to display the current connection status of the server.
                connection.ConnectionStatusChanged += new EventHandler<ConnectionStatusChangedEventArgs>(connection_ConnectionStatusChanged);
                connection_ConnectionStatusChanged(connection, new ConnectionStatusChangedEventArgs { Status = connection.Status });
                remote = new Remote(connection, form.Settings.CreateServer());
                // subscribing to this event will let us know when there is an error with the remote issuing the command.
                remote.ErrorOccurred += new EventHandler<RemoteErrorEventArgs>(remote_ErrorOccurred);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (remote != null) remote.Dispose();
            base.OnClosing(e);
        }
        #endregion

        #region Remote Events
        void remote_ErrorOccurred(object sender, RemoteErrorEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ErrorOccurredDelegate(remote_ErrorOccurred), sender, e);
                return;
            }

            MessageBox.Show(
                string.Format (
                    "Could not communicate with server. The following error occurred:\n{0}\n\n" + 
                    "If the problem persists, please restart the listening process and try again.",
                    e.Error.Message
                    ),
                "VMC Remote",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        void connection_ConnectionStatusChanged(object sender, ConnectionStatusChangedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new StatusChangedDelegate(connection_ConnectionStatusChanged), sender, e);
                return;
            }

            switch (e.Status)
            {
                case ConnectionStatus.Open:
                    connectingPanel.Visible = false;
                    base.Enabled = true;
                    connectionStatusLabel.Text = "Connected";
                    break;
                case ConnectionStatus.Closed:
                    connectingPanel.Visible = false;
                    base.Enabled = true;
                    connectionStatusLabel.Text = "(Connection Closed)";
                    break;
                case ConnectionStatus.Connecting:
                    base.Enabled = false;
                    connectingPanel.Visible = true;
                    connectingPanel.Enabled = true;
                    connectionStatusLabel.Text = "Connecting...";
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Button Events (there's lots of 'em)

        #region Special Button Events
        private void homeButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Home);
        }

        private void liveTvButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.LiveTv);
        }

        private void guideButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Guide);
        }

        private void recordedTvButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.RecordedTv);
        }

        private void musicButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Music);
        }

        private void videosButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Videos);
        }

        private void picturesButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Pictures);
        }
        #endregion

        #region Other Button Events
        private void backButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Back);
        }

        private void infoButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.ContextMenu);
        }
        #endregion

        #region TV Button Events
        private void recordButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.RecordedTv);
            remote.ExecuteCommand(RemoteCommand.RecordedTv);
        }

        private void channelUpButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.ChannelUp);
        }

        private void channelDownButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.ChannelDown);
        }
        #endregion

        #region Audio/Video Button Events
        private void volumeUpButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.VolumeUp);
        }

        private void volumeDownButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.VolumeDown);
        }

        private void muteButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Mute);
        }

        private void skipBackButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.SkipBack);
        }

        private void skipForwardButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.SkipForward);
        }

        private void rewindButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Rewind);
        }

        private void fastForwardButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.FastForward);
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.PlayPause);
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Play);
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Stop);
        }
        #endregion

        #region More Button Events
        private void startMceButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.StartMediaCenter);
        }

        private void closeMceButton_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.CloseMediaCenter);
        }
        #endregion

        #region Key Events
        private void RemoteForm_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                remote.ExecuteCommand(RemoteCommand.MoveUp);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                remote.ExecuteCommand(RemoteCommand.MoveDown);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                remote.ExecuteCommand(RemoteCommand.MoveLeft);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                remote.ExecuteCommand(RemoteCommand.MoveRight);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                remote.ExecuteCommand(RemoteCommand.Ok);
            }
        }
        #endregion

        #region Power Button Events
        private void sleepMenuItem_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Sleep);
        }

        private void restartMenuItem_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.Restart);
        }

        private void shutDownMenuItem_Click(object sender, EventArgs e)
        {
            remote.ExecuteCommand(RemoteCommand.ShutDown);
        }
        #endregion

        #region Remote Button Events
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #endregion
    }
}